// pages/review/review.js
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        switchValue:false,
        stars: [0, 1, 2, 3, 4],
        normalsrc: 'https://2016.bookgo.com.cn/static/wxapp/images/normal.png',
        selectedsrc: 'https://2016.bookgo.com.cn/static/wxapp/images/selected.png',
        key: 0,//评分
        commentShow:false,
        aid:'',
        reader_id:'',
        isbn:'',
        comment:'',
        selected:false
    },


    select: function (e) {
        // console.log(e);
        var key = e.currentTarget.dataset.key
        console.log(key);
        this.setData({
            key: key
        })
    },

    //是否匿名
    bindCheckbox: function (e) {
        console.log('------bindCheckbox---->e---------');
        console.log(e);
        var selected = e.currentTarget.dataset.selected;
        this.setData({
            selected: selected,
        })
    },
    
    formSubmit: function (e) {
        console.log(e);
        var that =this;
        var comment = that.data.comment,
            comment = e.detail.value.evaContent,
            key = that.data.key,
            aid = that.data.aid,
            isbn = that.data.isbn,
            reader_id = wx.getStorageSync('user').reader_id,
            openid = wx.getStorageSync('openid');
        var userInfo = wx.getStorageSync('userInfo'),
            avatarUrl = userInfo.avatarUrl,
            nickname = userInfo.nickName,
            userinfo = wx.getStorageSync('userinfo'),
            name = userinfo.name;
            console.log('---------name-----------');
            console.log(name);
            console.log(key);
            console.log(that.data.switchValue);
            console.log(aid);
            console.log(isbn);
            console.log(reader_id);
            console.log(openid);
            console.log(nickname);
            console.log(avatarUrl);
        if (comment != ''){
            if (that.data.selected == false) {
                name = name;
            } else {
                var name = '';
            }
            wx.request({
                url: 'https://2016.bookgo.com.cn/book/apiapp/comment',
                data: {
                    comment: comment,
                    key: key,
                    name: name,
                    nickname: nickname,
                    aid: aid,
                    reader_id: reader_id,
                    openid: openid,
                    avatarUrl: avatarUrl,
                    isbn: isbn
                },
                success: function (res) {
                    console.log(res);
                    if (res.data.code == 101) {
                        that.setData({
                            commentShow: true,
                            comment:comment,
                            key:''
                        })
                    }
                }
            })
        }else{
            wx.showModal({
                title: '提示',
                content: '评论内容不能为空',
            })
        }
        
        
    },
    switchChange: function (options) {
        console.log(options);
        var switchValue = options.detail.value;
        this.setData({
            switchValue: switchValue
        })
    },

    //关闭弹窗
    close_comment: function (e) {
        console.log(e);
        var that = this;
        that.setData({
            commentShow:false
        })
    },


    //查看详情
    look_detail: function () {
        const that = this;
        wx.navigateTo({
            url: '/bookgo/borrow/list/review/review_detail/review_detail?isbn=' + that.data.isbn 
    })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log('-----------review--->onLoad--------------');
        console.log(options);
        this.setData({
            aid:options.aid,
            isbn:options.isbn,
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})